﻿''============================================================================
''PTIC_composants_WF - Bibliothèque de composants graphiques
''Copyright © 2010 PIERRE TISSOT CONSEIL
''=============================================================================

Imports System.Drawing.Drawing2D
Imports System.ComponentModel

'Label permettant :
'   - d'afficher un fonds dégradé
'   - de personnaliser les bordures
Public Class PTIC_Label
    Inherits System.Windows.Forms.Label


#Region "Enumérations"
    Public Enum nomThemeLabel
        OMANE
        OMANEtxtBoxNonModif
        Standard
    End Enum
#End Region


#Region "Variables de classe"

    'Gestion du thème
    Private _theme As ThemeComposant
    Private _nomTheme As nomThemeLabel

    'Gestion des personnalisation de thème
    '- Dégradé
    Private _backColorDegrade As Boolean
    Private _backColorDegrade1 As Color
    Private _backColorDegrade2 As Color
    '- Bordures
    Private _borderPerso As Boolean
    Private _BorderColor_Left As Color
    Private _BorderColor_top As Color
    Private _BorderColor_Right As Color
    Private _BorderColor_Bottom As Color
    '- Police
    Private _fontPerso As Boolean
    '- Couleur du texte
    Private _foreColorPerso As Boolean

#End Region

#Region "Propriétés"

#Region "Personnalisation du thème"
    <Browsable(True), Category("Thème - Personnalisation")> _
 Public Property backColorDegrade() As Boolean
        Get
            Return _backColorDegrade
        End Get
        Set(ByVal value As Boolean)
            _backColorDegrade = value
        End Set
    End Property
    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property backColorDegrade1() As Color
        Get
            Return _backColorDegrade1
        End Get
        Set(ByVal value As Color)
            _backColorDegrade1 = value
        End Set
    End Property
    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property backColorDegrade2() As Color
        Get
            Return _backColorDegrade2
        End Get
        Set(ByVal value As Color)
            _backColorDegrade2 = value
        End Set
    End Property

    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property borderPerso() As Boolean
        Get
            Return _borderPerso
        End Get
        Set(ByVal value As Boolean)
            _borderPerso = value
        End Set
    End Property
    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property borderColor_left() As Color
        Get
            Return _BorderColor_Left
        End Get
        Set(ByVal value As Color)
            _BorderColor_Left = value
        End Set
    End Property
    <Browsable(True), Category("Thème - Personnalisation")> _
  Public Property borderColor_top() As Color
        Get
            Return _BorderColor_top
        End Get
        Set(ByVal value As Color)
            _BorderColor_top = value
        End Set
    End Property
    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property borderColor_right() As Color
        Get
            Return _BorderColor_Right
        End Get
        Set(ByVal value As Color)
            _BorderColor_Right = value
        End Set
    End Property
    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property borderColor_bottom() As Color
        Get
            Return _BorderColor_Bottom
        End Get
        Set(ByVal value As Color)
            _BorderColor_Bottom = value
        End Set
    End Property

    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property fontPerso() As Boolean
        Get
            Return _fontPerso
        End Get
        Set(ByVal value As Boolean)
            _fontPerso = value
            Me.Invalidate()
        End Set
    End Property
    <Browsable(True), Category("Thème - Personnalisation")> _
    Public Property foreColorPerso() As Boolean
        Get
            Return _foreColorPerso
        End Get
        Set(ByVal value As Boolean)
            _foreColorPerso = value
            Me.Invalidate()
        End Set
    End Property
#End Region

    <Browsable(True), Category("Thème")> _
    Public Property nomStyle() As nomThemeLabel

        Get
            Return _nomTheme
        End Get

        Set(ByVal value As nomThemeLabel)

            Dim etat As etatComposant
            If Me.Enabled Then etat = etatComposant.Enabled Else etat = etatComposant.Disabled

            _nomTheme = value

            If value = nomThemeLabel.OMANE Then

                Dim styleOMANElblSTD As _
                        New styleEtatComposant(My.Settings.OMANELblSTD_ColorDegradeStandard1, _
                                               My.Settings.OMANELblSTD_ColorDegradeStandard2, _
                                               My.Settings.OMANELblSTD_Font, _
                                               My.Settings.OMANE_STDForeColor, _
                                               My.Settings.OMANE_ColorBorder, _
                                               My.Settings.OMANE_AngleRoundRect)

                _theme = New ThemeComposant(styleOMANElblSTD)

            End If

            If value = nomThemeLabel.OMANEtxtBoxNonModif Then

                Dim styleOMANESTD As _
                        New styleEtatComposant(Nothing, _
                                               Nothing, _
                                               My.Settings.OMANELblSTD_Font, _
                                               My.Settings.OMANE_STDForeColor, _
                                               My.Settings.OMANEBtnSTD_ColorDegrade, _
                                               My.Settings.OMANE_AngleRoundRect)

                _theme = New ThemeComposant(styleOMANESTD)

            End If

            Invalidate()

        End Set

    End Property


#End Region

#Region "Constructeur"
    Public Sub New()

        'Activation du double buffering
        ''SetStyle(ControlStyles.UserPaint, True)
        ''SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        ''SetStyle(ControlStyles.OptimizedDoubleBuffer, True)

        nomStyle = nomThemeLabel.OMANE
        fontPerso = False
        foreColorPerso = False
        borderPerso = False
        backColorDegrade = False

    End Sub
#End Region

#Region "Redéfinition de la méthode Paint"

    'Couleur 1 du dégradé : (227, 239, 255)
    'Couleur 2 du dégradé : (175, 210, 255)

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)

        If Not (_backColorDegrade Or _nomTheme = nomThemeLabel.OMANE) Then

            MyBase.OnPaint(e)

        Else

            Dim colorDegrade1 As Color = IIf(_backColorDegrade, _backColorDegrade1, _theme.styleEnCours.backColor)
            Dim colorDegrade2 As Color = IIf(_backColorDegrade, _backColorDegrade2, _theme.styleEnCours.backColor2)
            Dim font As Font = IIf(_fontPerso, Me.Font, _theme.styleEnCours.font)
            Dim foreColor As Color = IIf(_foreColorPerso, Me.ForeColor, _theme.styleEnCours.foreColor)

            'Fonction permettant de faire le dégradé de base sur le label (style outlook)
            Dim rect As RectangleF = New RectangleF(2, 2, Me.Width, Me.Height)
            Dim lgb As LinearGradientBrush = New LinearGradientBrush(rect, colorDegrade1, colorDegrade2, LinearGradientMode.Vertical)
            e.Graphics.FillRectangle(lgb, rect)

            'Fonction permettant de tracer la 2nd bordure blanche
            e.Graphics.DrawLine(New Pen(Color.White, 1), 1, 1, Me.Width, 1)
            e.Graphics.DrawLine(New Pen(Color.White, 1), 1, 1, 1, Me.Height)

            'Tracé du texte
            Dim SizeTxt As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
            e.Graphics.DrawString(Me.Text, font, New SolidBrush(foreColor), 5, (Me.Height - SizeTxt.Height) / 2)

        End If

        If _borderPerso Then
            'Fonction permettant de tracer les bordures
            'Définition du rectangle à entourer de bordures
            Dim rcItem As Rectangle = New Rectangle(0, 0, Me.Bounds.Width, Me.Bounds.Height)
            'Bordure gauche
            e.Graphics.DrawLine(New Pen(_BorderColor_Left, 1), rcItem.X, rcItem.Y + rcItem.Height - 1, rcItem.X, rcItem.Y)
            'Bordure haut
            e.Graphics.DrawLine(New Pen(_BorderColor_top, 1), rcItem.X, rcItem.Y, rcItem.X + rcItem.Width - 1, rcItem.Y)
            If Not (rcItem.Width = 0) Then
                'Bordure droite
                e.Graphics.DrawLine(New Pen(_BorderColor_Right, 1), rcItem.X + rcItem.Width - 1, rcItem.Y, rcItem.X + rcItem.Width - 1, rcItem.Top + rcItem.Height - 1)
                'Bordure bas
                e.Graphics.DrawLine(New Pen(_BorderColor_Bottom, 1), rcItem.X + rcItem.Width - 1, rcItem.Top + rcItem.Height - 1, rcItem.X, rcItem.Top + rcItem.Height - 1)
            End If
        End If

        If _nomTheme = nomThemeLabel.OMANE Or (_nomTheme = nomThemeLabel.OMANEtxtBoxNonModif And Me.Enabled) Then
            Dim lpen As New Pen(_theme.styleEnCours.borderColor, 1)
            e.Graphics.DrawLine(lpen, 0, 0, 0, Me.Bounds.Height - 1)
            e.Graphics.DrawLine(lpen, 0, 0, Me.Bounds.Width - 1, 0)
            e.Graphics.DrawLine(lpen, Me.Bounds.Width - 1, 0, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
            If _nomTheme = nomThemeLabel.OMANEtxtBoxNonModif Then
                e.Graphics.DrawLine(lpen, 0, Me.Bounds.Height - 1, Me.Bounds.Width - 1, Me.Bounds.Height - 1)
            End If
            lpen.Dispose()
        End If

    End Sub

#End Region

End Class
